package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: LuoXuhuan
 * @date: 2020/10/28-19:07
 * @Version: 1.0.0
 */
@Service
@Slf4j
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;


    @Override
    public List<Role> findRole() {

        List<Role> role = roleDao.findRole();

        return role;
    }
    /**
     * 新增角色
     * @param role
     * @param
     */
    @Override
    public void add(Role role, Integer[] permissionsIds, Integer[] menusIds) {

        roleDao.add(role);

        if(role.getId() != null){
            setRoleAndPerssionRelation(role.getId(), permissionsIds);
        }
        if(role.getId() != null){
            setRoleAndMenuRelation(role.getId(), menusIds);
        }
    }

    /**
     * 编辑角色
     * @param role
     * @param
     */
    @Override
    public void edit(Role role, Integer[] permissionsIds, Integer[] menusIds) {

        //1.修改角色表
        roleDao.edit(role);

      //删除关联表
        roleDao.delMenuRelation(role.getId());
        roleDao.delPermissionRelation(role.getId());

        //重新建立关联表
        if(role.getId() != null){
            setRoleAndPerssionRelation(role.getId(), permissionsIds);
        }

        if(role.getId() != null){
            setRoleAndMenuRelation(role.getId(), menusIds);
        }

    }

    /**
     * 查找权限id按照角色id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionById(Integer id) {
        List<Integer> permissions=roleDao.findPermissionById(id);
        return permissions;
    }

    /**
     * 查找菜单id按照角色id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuById(Integer id) {
        List<Integer> menus =roleDao.findMenuById(id);
        return menus;
    }

    @Override
    public Role findById(Integer id) {
        Role role=roleDao.findById(id);
        return role;
    }
    /**
     * 1.查询关系表判断是否被关联
     * 如果被关联,根据角色id删除菜单表关系,再根据角色id删除权限中间表
     * 删除角色表
     *
     * @param id
     */
    @Override
    public void delById(Integer id) {
        long userIds = roleDao.findRelationUser(id);
        if (userIds > 0) {
            throw new BusinessRuntimeException("该角色被用户关联,不能删除");
        }
        roleDao.delRelationMenu(id);
        roleDao.delRelationPer(id);
        roleDao.delById(id);
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2. 条件查询
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{" + page + "}");
        //封装PageResult
        return new PageResult(page.getTotal(), page);
    }



    //维护角色和权限的关系
    public void setRoleAndPerssionRelation(Integer roleId ,Integer[] perssionItemIds){

        if( perssionItemIds != null && perssionItemIds.length > 0){
            for (Integer perssionItemId : perssionItemIds) {
                roleDao.setRoleAndPerssionRelation(roleId ,perssionItemId );
            }
        }
    }

    //维护角色和菜单项的关系
    public void setRoleAndMenuRelation(Integer roleId ,Integer[] menuItemIds){

        if( menuItemIds != null && menuItemIds.length > 0){
            for (Integer menuItemId : menuItemIds) {
                roleDao.setRoleAndMenuRelation(roleId ,menuItemId);
            }
        }
    }

}
